import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/Login.vue'
import GoodsList from '../views/GoodsList.vue'
import GoodsDetail from '../views/GoodsDetail.vue'
import User from '../views/User.vue'
import Waiter from '../views/Waiter.vue'
import SubmitFly from "@/views/SubmitFly.vue";
Vue.use(VueRouter)


const routes = [
  {
    path: '/login',
    name: 'Login',
    component: Login,
    meta:{
      showTabbar:false,
      title:'登录'
    }
  },
  {
    path: '/',
    name: 'GoodList',
    component: GoodsList,
    meta:{
      showTabbar:true,
      title:'商品列表'
    }
  },
  {
    path: '/detail',
    name: 'GoodDetail',
    component: GoodsDetail,
    meta:{
      showTabbar:true,
      title:'商品详情'
    }
  },
  {
    path: '/submitFly',
    name: 'SubmitFly',
    component:SubmitFly,
    meta:{
      showTabbar:true,
      title:'自助积分'
    }
  },
  {
    path: '/user',
    name: 'User',
    component: User,
    meta:{
      showTabbar:true,
      title:'用户信息'
    }
  },
  {
    path: '/waiter',
    name: 'Waiter',
    component: Waiter,
    meta:{
      showTabbar:true,
      title:'客服'
    }
  }
]

const router = new VueRouter({
  routes
})

// 解决Vue-Router升级导致的Uncaught(in promise) navigation guard问题
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (location, onResolve, onReject) {
  if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
  return originalPush.call(this, location).catch(err => err)
}

router.beforeEach((to,from,next)=>{
  document.title = to.meta.title
    //console.log('local',localStorage.getItem('userId')===null)
  if( ((to.name==='GoodDetail' || to.name==='User') || to.name==='SubmitFly') &&
      (localStorage.getItem('userId')===null || localStorage.getItem('userId')==='')){
    next('/login')
  }else{
    next()
  }

})

export default router
